/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.drypisy;

import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 * Controler class. Bridge between GUI and ImagePanel class.
 * @author Marcin
 */
public class AppControler {
    private ImageProcess proc;
    private BufferedImage image;
    private BufferedImage imageOrg;
    private ImagePanel panel;
    private AppGui gui;
    private File openedFile;
    private FileNameExtensionFilter opForm;
    
    /**
     * Constructor.
     * @param pan reference to ImagePanel
     * @param app reference to GUI object
     */
    public AppControler(ImagePanel pan, AppGui app){
        panel = pan;
        gui = app;
    }
    /**
     * 
     * @param p chosen proces object
     */
    public void setProcess(ImageProcess p){
        proc = p;
    }
    /**
     * Run process on actual image and paint results.
     */
    public void process(){
        image = proc.process(image);
        image = proc.clippImage(image);
        panel.setImage(image);
        gui.repaint();
    }
    /**
     * Returns dimension of image.
     * @return Image dimension.
     */
    public Dimension getImageDimension(){
        return new Dimension(image.getWidth(), image.getHeight());
    }
    /**
     * Return image.
     * @return 
     */
    public BufferedImage getImage(){
        return image;
    }
    /**
     * Load image from file.
     * @param f File which content image.
     */
    public void loadImage(File f, FileNameExtensionFilter form){
        try {
            image = ImageIO.read(f);
            opForm = form;
            imageOrg = ImageIO.read(f);
            panel.setImage(image);
            openedFile = f;
        } catch (IOException ex) {
            Logger.getLogger(AppControler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    /**
     * Save image in file.
     * @param path Path to file
     * @param form File format (jpg, gif, png...)
     */
    public void saveImage(String path, String form){
        try {
            if(!path.contains(".")){
                path += "." + form;
            }
            if(form.equalsIgnoreCase("jpeg") || form.equalsIgnoreCase("jpg")){
                ImageIO.write(changeFormat(), form, new File(path));
                System.out.println("jpeg");
            }else{
                System.out.println("non jpeg");
                ImageIO.write(image, form, new File(path));
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    private BufferedImage changeFormat(){
        int width = image.getWidth();
        int height = image.getHeight();
        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        //int tmp[] = image.getRGB(0, 0, width, height, null, 0, width);
        //img.setRGB(0, 0, width, height, tmp, 0, width);
        img.getGraphics().drawImage(image, 0, 0, null);
        return img;
    }
    
    public void saveImage(){
        try {
            System.out.println( "roz: " + opForm.getExtensions()[0]);
            String form = opForm.getExtensions()[0];
            if(form.equalsIgnoreCase("jpeg") || form.equalsIgnoreCase("jpg")){
                ImageIO.write(changeFormat(), form, openedFile);
                System.out.println("jpeg");
            }else{
                System.out.println("non jpeg");
                ImageIO.write(image, form, openedFile);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    /**
     * Undo all changes made on image.
     */
    public void backToOriginalImg(){
        ColorModel cm = imageOrg.getColorModel();
        boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
        WritableRaster raster = imageOrg.copyData(null);
        image =  new BufferedImage(cm, raster, isAlphaPremultiplied, null);
        panel.setImage(image);
        gui.repaint();
    }
}